package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.*;
import server.business.service.*;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.JsonResult;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

//myq
@RestController
@RequestMapping("/transfusionClinic")
public class TransfusionClinicController {
    @Autowired
    private TransfusionClinicService transfusionClinicService;
    @Autowired
    private BloodTestService bloodTestService;
    @Autowired
    private TransfusionReviewService transfusionReviewService;
    @Autowired
    private DiseaseTypeService diseaseTypeService;
    @Autowired
    private TransfusionStateService transfusionStateService;

    @Autowired
    private PatientService patientService;

    // *************************************myq*************************start
    @RequestMapping("/normal/list")
    @ResponseBody
    public JsonResult<Object> listPage() {
        Page<TransfusionReview> pageList = transfusionReviewService.getClinicPage();
        return JsonResult.success(pageList);
    }

    /**
     * 临床输血申请提交生成审核记录
     *
     * @param transfusionClinic
     * @return
     */
    @RequestMapping(value = "/normal/addformdata", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> saveClinicApplication(@RequestBody @Valid TransfusionClinic transfusionClinic,
            TransfusionReview transfusionReview) {
        System.out.println(transfusionClinic.getPatientIdcard());
        long transfusionNumberId = Long.parseLong(transfusionClinic.getPatientIdcard());
        GenerateNumberBySnowFlake transfusionNumber1 = new GenerateNumberBySnowFlake(transfusionNumberId, "C");
        transfusionClinic.setTransfusionNumber(transfusionNumber1.genNumberByflag());
        System.out.println(transfusionNumber1.genNumberByflag());

        transfusionClinicService.tranClinicAdd(transfusionClinic);
        String transfusionNumber = transfusionClinic.getTransfusionNumber();
        System.out.println(transfusionNumber);
        transfusionReviewService.insertRecord(transfusionNumber);

        transfusionReview.setState("1");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);

        return JsonResult.success();
    }

    /**
     * 点击审核展示输血申请单信息(只根据输血申请单号)
     */
    @PostMapping("/normal/applyList")
    @ResponseBody
    public JsonResult<Object> getClinicInfo(HttpServletRequest httpServletRequest) {
        String transfusionNumber = httpServletRequest.getParameter("transfusionNumber");
        System.out.println(transfusionClinicService.getClinicInfo(transfusionNumber));
        ArrayList<Object> listOne = transfusionClinicService.getClinicInfo(transfusionNumber);
        return JsonResult.success(listOne);
    }

    /**
     * 根据血液检验单号回填检验信息
     */
    @PostMapping("/normal/applyList2")
    @ResponseBody
    public JsonResult<Object> getBloodInfo1(HttpServletRequest httpServletRequest) {
        String inspectionSheet = httpServletRequest.getParameter("inspectionSheet");
        System.out.println(bloodTestService.getBloodInfo(inspectionSheet));
        return JsonResult.success(bloodTestService.getBloodInfo(inspectionSheet));
    }

    /**
     * 临床输血申请审批
     */
    @RequestMapping(value = "/review/pass", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> reviewClinicApplication(@RequestBody @Valid TransfusionClinic transfusionClinic,
            TransfusionReview transfusionReview) {
        String transfusionNumber = transfusionClinic.getTransfusionNumber();
        transfusionReview.setState("2");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        return JsonResult.success();
    }

    /**
     * 临床输血申请驳回
     *
     */
    @RequestMapping(value = "/review/reject", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult<Object> clinicReject(@RequestBody(required = false) @Valid TransfusionClinic transfusionClinic,
            TransfusionReview transfusionReview) {
        String transfusionNumber = transfusionClinic.getTransfusionNumber();
        System.out.println(transfusionNumber);
        transfusionReview.setState("3");
        transfusionReviewService.updateReview(transfusionNumber, transfusionReview);
        return JsonResult.success();
    }

    /**
     * 获取申请数据字典
     * 
     * @return
     */
    @GetMapping("/normal/typelist")
    @ResponseBody
    public List<TransfusionState> getTypeInfo() {
        return transfusionStateService.list();
    }

    /**
     * 获取临床诊断数据字典
     * 
     * @return
     */
    @GetMapping("/normal/diagnosis")
    @ResponseBody
    public List<DiseaseType> getDiagnosisInfo() {
        return diseaseTypeService.list();
    }

    /**
     * 根据输血申请单号找到血液检验信息
     */
    @PostMapping("/normal/bloodList")
    @ResponseBody
    public JsonResult<Object> getBloodInfo(HttpServletRequest httpServletRequest) {
        String transfusionNumber = httpServletRequest.getParameter("transfusionNumber");
        System.out.println(transfusionClinicService.getClinicInfo(transfusionNumber));
        ArrayList<Object> listTwo = bloodTestService.getTestInfo(transfusionNumber);
        System.out.println(listTwo);
        return JsonResult.success(listTwo);
    }

    /**
     * 分页查询
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    @ResponseBody
    public JsonResult<Object> getPage(@PathVariable int currentPage, @PathVariable int pageSize,
            TransfusionReview transfusionReview, String startTime, String endTime, String reviewStartTime,
            String reviewEndTime) {
        IPage<TransfusionReview> page = transfusionReviewService.getSelectClinicPage(currentPage, pageSize,
                transfusionReview, startTime, endTime, reviewStartTime, reviewEndTime);
        if (currentPage > page.getPages()) {
            page = transfusionReviewService.getSelectClinicPage((int) page.getPages(), pageSize, transfusionReview,
                    startTime, endTime, reviewStartTime, reviewEndTime);
        }
        return JsonResult.success(page);
    }

    /**
     * 输入病历号自动回填身份证号（临床输血申请）
     */
    @RequestMapping(value = "/normal/getIdcard",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult<Object> getIdcard(HttpServletRequest httpServletRequest){
        System.out.println(httpServletRequest.getParameter("medicalRecordNumber"));
        String medicalRecordNumber=httpServletRequest.getParameter("medicalRecordNumber");
        System.out.println(patientService.getIdcard(medicalRecordNumber));
        return JsonResult.success( patientService.getIdcard(medicalRecordNumber));
    }

    /**
     * 批量删除
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public JsonResult<Object> batchDelete(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        ArrayList<Long> transfusionIds = transfusionClinicService.selectId(ids);
        transfusionClinicService.removeByIds(transfusionIds);
        boolean success = transfusionClinicService.removeByIds(ids);
        if (success) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }
    // *************************************myq*************************end

    // *************************************lrz*************************start
    @GetMapping("normal/lrz/{currentPage}/{pageSize}")
    @ResponseBody
    public Result getPage1(@PathVariable int currentPage, @PathVariable int pageSize,
            TransfusionClinic transfusionClinic) {
        IPage<TransfusionClinic> page = transfusionClinicService.getPage1(currentPage, pageSize,
                transfusionClinic);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = transfusionClinicService.getPage1((int) page.getPages(), pageSize, transfusionClinic);
        }
        return Result.success(page);
    }

    /**
     * 获得血型
     *
     * @param
     * @return
     */
    @GetMapping(value = "/normal/getBloodType")
    @ResponseBody
    public Result getBloodType(String patientIdcard) {
        // 首先根据身份证号在patient表中找到患者id
        Long patientId = patientService.getIdByIdcard(patientIdcard);
        Result result = new Result();
        if (patientId == null) {
            result.setMsg("未找到病人信息");
            result.setCode("500");
        }
        // 再用patient_id在blood_test表中找到患者血型
        String bloodType = bloodTestService.getBloodType(patientId);
        String rhBloodType = bloodTestService.getRhBloodType(patientId);
        if (bloodType == null || rhBloodType == null) {
            result.setMsg("未找到病人血型信息");
            result.setCode("500");
            return result;
        }
        BloodInformation bloodInformation = new BloodInformation();
        bloodInformation.setAboBloodType(bloodType);
        bloodInformation.setRhBloodType(rhBloodType);
        return Result.success(bloodInformation);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/normal/batchDelete1")
    @ResponseBody
    public Result batchDelete1(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean success = transfusionClinicService.removeByIds(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }
    // *************************************lrz*************************end
}
